package ua.kiev.tosik.price.gui;

import org.apache.log4j.Logger;
import org.jdesktop.swingx.JXTitledPanel;
import org.pushingpixels.substance.api.SubstanceLookAndFeel;
import org.pushingpixels.substance.api.tabbed.TabCloseListener;
import ua.kiev.tosik.price.IChapter;
import ua.kiev.tosik.price.conf.AppConfig;
import ua.kiev.tosik.price.conf.Language;

import javax.swing.*;
import java.awt.*;
import java.util.HashMap;

public class CenterPanel {

	private static CenterPanel ourInstance;
	private JXTitledPanel panel;
	private JTabbedPane centerPanel;
	private static Logger logger = Logger.getLogger(CenterPanel.class);
	private HashMap<String, IChapter> tabList = new HashMap<String, IChapter>();

	public static CenterPanel getInstance() {
		if (ourInstance == null) {
			ourInstance = new CenterPanel();
		}
		return ourInstance;
	}

	private CenterPanel() {
		panel = new JXTitledPanel();
		centerPanel = new JTabbedPane(JTabbedPane.TOP);
		centerPanel.putClientProperty(SubstanceLookAndFeel.TABBED_PANE_CLOSE_BUTTONS_PROPERTY, Boolean.TRUE); // This is for showing tab close button
		SubstanceLookAndFeel.registerTabCloseChangeListener(centerPanel, new TabCloseListener() {
			private String tabName;
			public void tabClosing(JTabbedPane tabbedPane, Component tabComponent) {
				logger.debug("Tab [" + tabbedPane.getToolTipTextAt(tabbedPane.indexOfComponent(tabComponent)) + "] closing");
				tabName = tabbedPane.getToolTipTextAt(tabbedPane.indexOfComponent(tabComponent));
			}
			public void tabClosed(JTabbedPane tabbedPane, Component tabComponent) {
				logger.debug("Tab closed");
				IChapter ch = tabList.get(tabName);
				tabList.remove(tabName);
				ch.destroy();
				// if (AppConfig.getInstance().get("app.ontabclose.free").equals("true")) {
				//	ch.destroy();
				// }
				tabName = null;
				AppConfig.getInstance().set("app.lasttab.class", "");
			}
		});
		panel.add(centerPanel);
		updateLocale();
	}

	public JXTitledPanel getPanel() {
		return panel;
	}

	public void closeTab(IChapter chapter) {
		String tabKey = chapter.getTabKey();
		logger.debug("Try to close " + tabKey);
		for ( int i = 0; i < centerPanel.getTabCount(); i++ ) {
			if ( centerPanel.getToolTipTextAt(i).equals(tabKey) ) {
				tabList.remove(tabKey);
				centerPanel.removeTabAt(i);
				if ( AppConfig.getInstance().get("app.ontabclose.free").equals("true") ) {
					chapter.destroy();
				}
				AppConfig.getInstance().set("app.lasttab.class", "");
				break;
			}
		}
	}

	public void addTab(IChapter chapter, ImageIcon icon) {
		String tabKey = getTabKey(chapter);
		chapter.setTabKey(tabKey);
		centerPanel.addTab(chapter.getTitle(), icon, chapter.getPanel(), tabKey);
		tabList.put(tabKey, chapter);
		centerPanel.setSelectedIndex(centerPanel.getTabCount()-1);
		AppConfig.getInstance().set("app.lasttab.class", chapter.getClass().getName());
		logger.debug("Tab [" + tabKey + "] created");
	}

	public HashMap<String, IChapter> getTabList() {
		return tabList;
	}

	public void updateLocale() {
		panel.setTitle(Language.getInstance().get("app.centerpanel.title"));
	}

	public int countOpenedTabByTitle(String title) {
		int count = 0;
		for ( int i = 0; i < centerPanel.getTabCount(); i++ ) {
			if ( title.equals(centerPanel.getTitleAt(i)) ) {
				count++;
			}
		}
		return count;
	}

	private String getTabKey(IChapter chapter) {
		return chapter.getTitle() + " (" + countOpenedTabByTitle(chapter.getTitle()) + ")";
	}

}
